package com.gitee.wsl.ext.collection


/**
 * Emits each distinct value with the number counts it appeared
 */
fun <T> Sequence<T>.countBy() =
    groupBy { it }
        .entries.asSequence()
        .map { it.key to it.value.count() }


fun <T> Iterable<T>.countBy() = asSequence().countBy()

fun <T,R> Sequence<T>.countBy(selector: (T) -> R) =
    groupBy { selector(it) }
        .entries.asSequence()
        .map { it.key to it.value.count() }

fun <T,R> Iterable<T>.countBy(selector: (T) -> R) = asSequence().countBy(selector)